package com.sht.dao.impl;

import com.sht.dao.StudentDao;
import com.sht.domain.Student;
import com.sht.util.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

/**
 * StudentDaoImpl
 *
 * @author SHT
 * @date 2021/10/2 11:13
 */
public class StudentDaoImpl implements StudentDao {

    private static final String SQLID_PREFIX = "com.sht.dao.StudentDao.";

    @Override
    public Student searchStudentById(Integer id) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        // 读取的sqlId = namespace + "." + id
        String sqlId = SQLID_PREFIX + "searchStudentById";
        Student student = sqlSession.selectOne(sqlId, 1001);
        sqlSession.close();
        return student;
    }

    @Override
    public int insertStudent(Student student) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        // 读取的sqlId = namespace + "." + id
        String sqlId = SQLID_PREFIX + "insertStudent";
        int rows = sqlSession.insert(sqlId, student);
        // 这里需要手动提交事务
        sqlSession.commit();
        sqlSession.close();
        return rows;
    }

    @Override
    public List<Student> searchStudentList() {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        // 读取的sqlId = namespace + "." + id
        String sqlId = SQLID_PREFIX + "searchStudentList";
        List<Student> students = sqlSession.selectList(sqlId);
        sqlSession.close();
        return students;
    }
}
